package com.jic.manager.controller.manage.sys;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.entity.SysRole;
import com.jic.manager.entity.SysRoleLog;
import com.jic.manager.request.SysRoleLogRequest;
import com.jic.manager.response.SysRoleLogResponse;
import com.jic.manager.service.SysRoleLogService;
import com.jic.manager.service.SysRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("sysRloeLog")
public class SysRloeLogController {

    @Resource
    private SysRoleLogService sysRoleLogService;
    @Resource
    private SysRoleService sysRoleService;

    @PostMapping("getSysRoleLogPageList")
    public RestResult<PageResult<SysRoleLogResponse>> getSysRoleLogPageList(@RequestBody SysRoleLogRequest sysRoleLogRequest){
        SysRoleLog sysRoleLog = new SysRoleLog();
        BeanUtils.copyProperties(sysRoleLogRequest,sysRoleLog);
        PageResult<SysRoleLog> sysRoleLogPageResult = sysRoleLogService.queryPage(sysRoleLog, new Page() {{
            setStart(sysRoleLogRequest.getStart());
            setLimit(sysRoleLogRequest.getLimit());
        }});
        List<SysRoleLog> rows = sysRoleLogPageResult.getRows();
        List<SysRoleLogResponse> res = new ArrayList<>();
        rows.forEach(sysRoleLogVo->{
            SysRole sysRole = sysRoleService.queryByPrimaryKey(sysRoleLogVo.getRoleId());
            SysRoleLogResponse sysRoleLogResponse = new SysRoleLogResponse();
            sysRoleLogResponse.setRoleName(sysRole.getRoleName());
            BeanUtils.copyProperties(sysRoleLogVo,sysRoleLogResponse);
            res.add(sysRoleLogResponse);
        });
        return RestResult.success(new PageResult<SysRoleLogResponse>(){{
            setRows(res);
            setTotal(sysRoleLogPageResult.getTotal());
        }});
    }
}
